﻿import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

Rectangle {
    id: titleBar
    color: "#3A3A3A"

    property string title: ""
    property string iconSource: ""
    property bool maximizeVisible: true
    property bool restoreVisible: false

    signal closeClicked
    signal minimizeClicked
    signal maximizeClicked
    signal restoreClicked
    signal doubleClicked

    RowLayout {
        anchors.fill: parent
        spacing: 0

        // 图标
        Image {
            Layout.leftMargin: 10
            Layout.preferredWidth: 20
            Layout.preferredHeight: 20
            source: iconSource
            fillMode: Image.PreserveAspectFit
        }

        // 标题
        Label {
            Layout.leftMargin: 10
            text: title
            color: "white"
            font.pixelSize: 12
            elide: Text.ElideRight
            Layout.fillWidth: true
        }

        // 窗口控制按钮
        Row {
            Layout.alignment: Qt.AlignRight
            spacing: 0

            // 最小化按钮
            TitleBarButton {
                iconSource: "qrc:/res/image/minimize.svg"
                onClicked: minimizeClicked()
            }

            // 最大化/还原按钮
            TitleBarButton {
                iconSource: "qrc:/res/image/maximize.svg"
                visible: maximizeVisible
                onClicked: maximizeClicked()
            }

            TitleBarButton {
                iconSource: "qrc:/res/image/restore.svg"
                visible: restoreVisible
                onClicked: restoreClicked()
            }

            // 关闭按钮
            TitleBarButton {
                iconSource: "qrc:/res/image/close.svg"
                hoverColor: "#E81123"
                onClicked: closeClicked()
            }
        }
    }

    // 拖动区域 - 修复后的版本
    MouseArea {
        id: dragArea
        anchors.fill: parent
        property point startPos: "0,0"

        onPressed: function(mouse) {
            startPos = Qt.point(mouse.x, mouse.y)
        }

        onPositionChanged: function(mouse) {
            if (pressed) {
                windowsNative.startSystemMove(root, Qt.point(mouse.x + dragArea.x, mouse.y + dragArea.y))
            }
        }

        onDoubleClicked: {
            doubleClicked()
        }
    }
}
